package cn.talk.socket.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class Client_listen implements Runnable {

    private Socket socket;
    private Client client;

    public Client_listen(Socket socket, Client client) {
        this.socket = socket;
        this.client = client;
    }

    @Override
    public void run() {
        try {
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                String mes = (String) ois.readObject();
                String[] strings = mes.split("-");
                if (strings[0].equals("restId")) {
                    System.out.println("收到来自" + strings[2] + "的信息为：" + strings[1]);
                    client.setId(strings[1]);
                } else {
                    String message = strings[0];
                    String source = strings[1];
                    String show = client.getId() + "收到来自" + source + "的信息为：" + message;
                    System.out.println(show);
                    client.messageList.add(show);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
